package com.ah.shanxi_demo.sync.utils;

import com.ah.shanxi_demo.entity.SxSync;
import com.ah.shanxi_demo.sync.param.result.SyncOrderResult;
import com.ah.shanxi_demo.sync.param.result.SyncPolicyResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import java.util.List;

import static com.ah.shanxi_demo.common.StrUtils.syncObjectToJsonObject;

/**
 * @author zt 2020/8/21
 */
public class syncResultConvertObject {
    private static final Logger log = LoggerFactory.getLogger(syncResultConvertObject.class);

    private static final String order_param = "{\"code\":0,\"work_list\":[{\"work_list\":[{\"accdept\":\"生成部门\",\"accuser\":\"生成人\",\"content\":\"接收实时日志数据异常\",\"level\":3,\"title\":\"接收实时日志数据异常\",\"type\":1,\"work_code\":\"11-20190710-1\",\"writer_user\":\"张三\"},{\"accdept\":\"生成部门\",\"accuser\":\"生成人\",\"content\":\"漏洞扫描\",\"level\":3,\"scan_type\":1,\"title\":\"进行漏洞扫描\",\"type\":5,\"work_code\":\"11-20190710-2\",\"writer_user\":\"张三\"}]}],\"msg\":\"SUCCESS\"}";

    public static final String sync_param="{\"msg\":\"成功\",\"workorder_state\":\"82d736e86d7f4a66ba1d6cc006d7d370\",\"code\":200,\"policy_state\":\"82d736e86d7f4a66ba1d6cc006d7d370\"}";

    public static void main(String[] args) {
        JSONObject jsonObject = syncObjectToJsonObject(sync_param);
        SxSync sync = sync_result_sync(jsonObject);
        log.info("sdk_sync【sync】：" + JSON.toJSONString(sync));
    }

    /**
     * 注册接口返回值解析(节点唯一标识)
     *
     * @param responseObject
     * @return
     */
    public static String sync_result_convert_centerKey(Object responseObject) {

        if (responseObject == null) {
            return null;
        }

        JSONObject jsonObject = syncObjectToJsonObject(responseObject);
        String code = objectToStr(jsonObject.get("code"));
        if (!HttpStatus.OK.toString().equals(code)) {
            return null;
        }

        String center_key = jsonObject.get("center_key").toString();

        return center_key;
    }


    /**
     * 同步接口返回值解析
     *
     * @param responseObject
     * @return
     */
    public static SxSync sync_result_sync(Object responseObject) {

        SxSync sync = new SxSync();

        if (responseObject == null) {
            return null;
        }
        JSONObject jsonObject = syncObjectToJsonObject(responseObject);
        String code = objectToStr(jsonObject.get("code"));
        if (!String.valueOf(HttpStatus.OK.value()).equals(code)) {
            return null;
        }

        sync.setCode(Integer.valueOf(code));
        sync.setWorkorderState(objectToStr(jsonObject.get("workorder_state")));
        sync.setPolicyState(objectToStr(jsonObject.get("policy_state")));

        return sync;
    }

    /**
     * 生成策略接口 返回值解析
     * <p>
     * 策略返回值实例
     * {"guid":"string","guid_type":"0","policy_code":"1102-2020","policy_list":[{"begin_time":"2020-02-02 02:27:37","end_time":"2020-02-02 02:27:37","log_type":"alarm","policy_type":"ALM-01-2"},{"begin_time":"2020-02-02 02:27:37","end_time":"2020-02-02 02:27:37","log_type":"alarm","policy_type":"ALM-01-1"}]}
     * {"code":200,"data":[{"guid":"string","guid_type":"0","policy_code":"1102-2020","policy_list":[{"begin_time":"2020-02-02 02:27:37","end_time":"2020-02-02 02:27:37","log_type":"alarm","policy_type":"ALM-01-2"},{"begin_time":"2020-02-02 02:27:37","end_time":"2020-02-02 02:27:37","log_type":"alarm","policy_type":"ALM-01-1"}]}],"msg":"SUCCESS"}
     *
     * @param responseObject
     * @return
     */
    public static List<SyncPolicyResult> sync_result_policy(Object responseObject) {

        if (responseObject == null) {
            return null;
        }

        JSONObject jsonObject = syncObjectToJsonObject(responseObject);

        String code = objectToStr(jsonObject.get("code"));
        if (!HttpStatus.OK.toString().equals(code)) {
            return null;
        }

        List<SyncPolicyResult> list = JSONObject.parseArray(objectToStr(jsonObject.get("data")), SyncPolicyResult.class);

        return list;
    }


    /**
     * 策略数据采集接口 返回值解析
     *
     * 工单返回值实例
     * {"code":200,"msg":"SUCCESS"}
     *
     * @param responseObject
     * @return
     */
    public static Boolean sync_result_policy_data(Object responseObject) {

        if (responseObject == null) {
            return null;
        }

        JSONObject jsonObject = syncObjectToJsonObject(responseObject);

        String code = objectToStr(jsonObject.get("code"));
        if (!HttpStatus.OK.toString().equals(code)) {
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }



    /**
     * 生成工单接口 返回值解析
     * <p>
     * 工单返回值实例
     * {"code":0,"work_list":[{"work_list":[{"accdept":"生成部门","accuser":"生成人","content":"接收实时日志数据异常","level":3,"title":"接收实时日志数据异常","type":1,"work_code":"11-20190710-1","writer_user":"张三"},{"accdept":"生成部门","accuser":"生成人","content":"漏洞扫描","level":3,"scan_type":1,"title":"进行漏洞扫描","type":5,"work_code":"11-20190710-2","writer_user":"张三"}]}],"msg":"SUCCESS"}
     *
     * @param responseObject
     * @return
     */
    public static List<SyncOrderResult> sync_result_order(Object responseObject) {

        if (responseObject == null) {
            return null;
        }

        JSONObject jsonObject = syncObjectToJsonObject(responseObject);

        String code = objectToStr(jsonObject.get("code"));
        if (!HttpStatus.OK.toString().equals(code)) {
            return null;
        }

        List<SyncOrderResult> list = JSONObject.parseArray(objectToStr(jsonObject.get("work_list")), SyncOrderResult.class);

        return list;
    }

    /**
     * 工单回执接口 返回值解析
     *
     * 工单返回值实例
     * {"code":200,"msg":"SUCCESS"}
     *
     * @param responseObject
     * @return
     */
    public static Boolean sync_result_order_receipt(Object responseObject) {

        if (responseObject == null) {
            return null;
        }

        JSONObject jsonObject = syncObjectToJsonObject(responseObject);

        String code = objectToStr(jsonObject.get("code"));
        if (!HttpStatus.OK.toString().equals(code)) {
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }






    /**
     * object 转换为 int
     *
     * @param object
     * @return
     */
    public Integer objectToInteger(Object object) {
        String objectToStr = objectToStr(object);
        if (StringUtils.isBlank(objectToStr)) {
            return 0;
        }
        return Integer.valueOf(objectToStr);
    }

    /**
     * object 转换为 string
     *
     * @param object
     * @return
     */
    public static String objectToStr(Object object) {
        if (null == object) {
            return "";
        }
        String objectStr = JSONObject.toJSONString(object);
        if (StringUtils.isBlank(objectStr)) {
            return "";
        }
        return objectStr.replace("\"","");
    }

}
